﻿/* Copyright (C) 2008 - 2011 by John Cronin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

namespace Kernel
{
    public class StaticArray
    {
        private ulong base_addr;
        private ulong length;
        private ulong ent_size;

        public StaticArray(ulong Base, ulong ByteLength)
        { base_addr = Base; length = ByteLength; ent_size = 1; }
        public StaticArray(ulong Base, ulong ByteLength, ulong ElemSize)
        { base_addr = Base; length = ByteLength; ent_size = ElemSize; }

        public void Clear(ushort v)
        {
            int j = 0;
            for (ulong i = 0; i < length; i++)
            {
                ABI.MemoryOperations.Poke(base_addr + i, (byte)(v >> (j * 8)));
                j++;
                if (j == 2)
                    j = 0;
            }
        }
        public void Clear(byte v)
        { for (ulong i = 0; i < length; i++) ABI.MemoryOperations.Poke(base_addr + i, v); }
        public void Clear()
        { Clear(0); }

        public void Set(ulong addr, byte v)
        {
            if ((addr * ent_size + 1) > length)
                throw new System.IndexOutOfRangeException();
            ABI.MemoryOperations.Poke(base_addr + addr * ent_size, v);
        }
        public void Set(ulong addr, ushort v)
        {
            if ((addr * ent_size + 2) > length)
                throw new System.IndexOutOfRangeException();
            ABI.MemoryOperations.Poke(base_addr + addr * ent_size, v);
        }
        public void Set(ulong addr, ulong v)
        {
            if ((addr * ent_size + 7) > length)
                throw new System.IndexOutOfRangeException();
            ABI.MemoryOperations.Poke(base_addr + addr * ent_size, v);
        }
        public ushort GetU2(ulong addr)
        {
            if ((addr * ent_size + 2) > length)
                throw new System.IndexOutOfRangeException();
            return ABI.MemoryOperations.PeekU2(base_addr + addr * ent_size);
        }
        public ulong GetU8(ulong addr)
        {
            if ((addr * ent_size + 7) > length)
                throw new System.IndexOutOfRangeException();
            return ABI.MemoryOperations.PeekU8(base_addr + addr * ent_size);
        }
        public StaticArray GetSA(ulong addr, ulong elem_size)
        {
            if ((addr * ent_size + (ent_size - 1)) > length)
                throw new System.IndexOutOfRangeException();
            return new StaticArray(base_addr + addr * ent_size, ent_size, elem_size);
        }

        public byte this[ulong i]
        {
            get
            {
                if (length <= i)
                    throw new System.IndexOutOfRangeException();
                return ABI.MemoryOperations.PeekU1(base_addr + i * ent_size);
            }

            set
            {
                if (i >= length)
                    throw new System.IndexOutOfRangeException();
                ABI.MemoryOperations.Poke(base_addr + i * ent_size, value);
            }
        }

    }
}
